package zhihui.mobile.presenter.shop;

import android.content.Context;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhihui.mobile.bean.shop.BannerInfo;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.bean.shop.detail.SpecifInfo;
import zhihui.mobile.bean.shop.detail.SpuDetailInfo;
import zhihui.mobile.constants.ActivitySourceTag;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.shop.ShopAttentionManager;
import zhihui.mobile.manager.shop.ShopManager;
import zhihui.mobile.manager.shop.ShopOrderManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.details.IShopDetailsContract;

/**
 * Created by 张公铭 on 2017/8/4 0004.
 */

public class ShopDetailsPresenter extends BaseShopPresenter implements IShopDetailsContract.IShopDetailsPresenter
{
    private Context mContext;
    private IShopDetailsContract.IShopDetailsView detailsView;
    private SpecifInfo mSpecifInfo;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        detailsView = (IShopDetailsContract.IShopDetailsView) view;
    }

    @Override
    public void onResume()
    {
        detailsView.refreshCartNum(ShopManager.getInstance().getCartInfo().getCartNum());
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void initViewData()
    {
        detailsView.refreshView(ShopManager.getInstance().getCurShop());
        List<BannerInfo> bannerInfos = new ArrayList<>();
        String[] iconAry = ShopManager.getInstance().getCurShop().getSpu().getRollimg().split(",");
        for (String icon : iconAry)
        {
            BannerInfo bannerInfo = new BannerInfo();
            bannerInfo.setImg(icon);
            bannerInfos.add(bannerInfo);
        }
        detailsView.refreshBanner(bannerInfos);
        if (zhihui.mobile.ui.utils.UiUtils.check(mContext))
        {
            List<SpecifInfo> specifInfos = ShopManager.getInstance().getCurShop().getSpecif();
            Map<String, List<SpecifInfo>> map = new HashMap<>();
            for (SpecifInfo specifInfo : specifInfos)
            {
                if (specifInfo.getSkuid() > -1)
                {
                    detailsView.initSpecification(specifInfo.getSpecifname(), specifInfo.getSepcifvalue(), specifInfo.getSkuid(), specifInfo.getPrice());
                    detailsView.updateBuyState(specifInfo.getSalestock());
                    mSpecifInfo = specifInfo;
                    return;
                }
            }
        }
    }

    @Override
    public void addShopCart(int skuid, final int num)
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext, ActivitySourceTag.ShopDetailsActivity))
        {
            return;
        }
        detailsView.showLoading();
        ShopManager.getInstance().addShopInCart(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                detailsView.hindLoading();
                detailsView.showMessage("加入购物车成功");
                int cartNum = ShopManager.getInstance().getCartInfo().getCartNum();
                cartNum += num;
                ShopManager.getInstance().getCartInfo().setCartNum(cartNum);
                detailsView.refreshCartNum(cartNum);
            }

            @Override
            public void failed(String message)
            {
                detailsView.hindLoading();
                detailsView.showMessage(message);
            }
        }, skuid, num);
    }

    @Override
    public void getSpecificationInfo(int type)
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext, ActivitySourceTag.ShopDetailsActivity))
        {
            return;
        }

        List<SpecifInfo> specifInfos = ShopManager.getInstance().getCurShop().getSpecif();
        Map<String, List<SpecifInfo>> map = new HashMap<>();
        for (SpecifInfo specifInfo : specifInfos)
        {
            if (!map.containsKey(specifInfo.getSpecifname()))
            {
                List<SpecifInfo> list = new ArrayList<>();
                list.add(specifInfo);
                map.put(specifInfo.getSpecifname(), list);
            } else
            {
                map.get(specifInfo.getSpecifname()).add(specifInfo);
            }
        }
        detailsView.showSpecificationDialog(map, type);
    }

    @Override
    public void goComfirmOrder(int skuId, int num)
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext, ActivitySourceTag.ShopDetailsActivity))
        {
            return;
        }
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setSkuId(skuId);
        skuInfo.setNum(num);
        skuInfo.setSalestock(mSpecifInfo.getSalestock());
        skuInfo.setSpuId(ShopManager.getInstance().getCurShop().getSpu().getId());
        for (SpecifInfo info : ShopManager.getInstance().getCurShop().getSpecif())
        {
            if (info.getSkuid() == skuId)
            {
                skuInfo.setPrice(info.getPrice());
                skuInfo.setCode(info.getSkucode());
                skuInfo.setThumbnail(info.getThumbnail());
                skuInfo.setSpecif(info.getSpecifname() + ":" + info.getSepcifvalue());
                skuInfo.setTitle(info.getSkutitle());
            }
        }
        List<SkuInfo> list = new ArrayList<>();
        list.add(skuInfo);
        ShopOrderManager.getInstance().setConfirmList(list);
        detailsView.goComfirmOrderActivity();
    }

    @Override
    public void attention()
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext))
        {
            return;
        }
        detailsView.showLoading();
        List<Object> list = new ArrayList<>();
        list.add(UserManager.getInstance().getCurUser().getId());
        list.add(ShopManager.getInstance().getCurShop().getSpu().getId());
        ShopAttentionManager.getInstance().attention(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                detailsView.hindLoading();
                detailsView.showMessage("关注成功");
            }

            @Override
            public void failed(String message)
            {
                detailsView.hindLoading();
                detailsView.showMessage(message);
            }
        }, list);
    }
}
